<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="年份" prop="years">
        <el-date-picker
          v-model="queryParams.years"
          value-format="YYYY"
          type="year"
          placeholder="选择年份"
          clearable
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="月份" prop="month">
        <el-date-picker
          v-model="queryParams.month"
          value-format="MM"
          type="month"
          placeholder="选择月份"
          clearable
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="员工姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入员工姓名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="发布状态" prop="publishStatus">
        <el-select
          v-model="queryParams.publishStatus"
          placeholder="请选择发布状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.OA_PUBLISH_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="序号" align="center" prop="id" width="60" />
      <el-table-column label="年份" align="center" prop="years" width="80" />
      <el-table-column label="月份" align="center" prop="month" width="80" />
      <el-table-column label="员工ID" align="center" prop="userId" width="100" />
      <el-table-column label="员工姓名" align="center" prop="userName" width="100" />
      <el-table-column label="公司" align="center" prop="company" width="120" />
      <el-table-column label="基本工资" align="center" prop="baseSalary" width="100" />
      <el-table-column label="岗位工资" align="center" prop="positionSalary" width="100" />
      <el-table-column label="工资小计" align="center" prop="salarySubtotal" width="100" />
      <el-table-column label="考勤天数" align="center" prop="attendanceDays" width="90" />
      <el-table-column label="公休/调休" align="center" prop="publicHolidayCompensatoryLeave" width="100" />
      <el-table-column label="病假" align="center" prop="sickLeave" width="80" />
      <el-table-column label="事假" align="center" prop="personalLeave" width="80" />
      <el-table-column label="其他" align="center" prop="otherLeave" width="80" />
      <el-table-column label="产检假" align="center" prop="maternityCheckupLeave" width="90" />
      <el-table-column label="扣除金额" align="center" prop="deductionAmount" width="100" />
      <el-table-column label="出勤天数" align="center" prop="actualAttendanceDays" width="90" />
      <el-table-column label="平日加班" align="center" prop="weekdayOvertime" width="90" />
      <el-table-column label="双休日加班" align="center" prop="weekendOvertime" width="100" />
      <el-table-column label="节假日加班" align="center" prop="holidayOvertime" width="100" />
      <el-table-column label="加班费" align="center" prop="overtimePay" width="90" />
      <el-table-column label="迟到早退" align="center" prop="lateEarlyDeduction" width="100" />
      <el-table-column label="错漏" align="center" prop="errorOmission" width="80" />
      <el-table-column label="其他扣款" align="center" prop="otherDeductions" width="100" />
      <el-table-column label="调整工资" align="center" prop="adjustedSalary" width="100" />
      <el-table-column label="绩效工资" align="center" prop="performanceSalary" width="100" />
      <el-table-column label="出差津贴" align="center" prop="businessTripAllowance" width="100" />
      <el-table-column label="高温补贴" align="center" prop="highTemperatureAllowance" width="100" />
      <el-table-column label="调整社保(扣)" align="center" prop="adjustedSocialSecurityDeduction" width="120" />
      <el-table-column label="外派补贴" align="center" prop="expatriateAllowance" width="100" />
      <el-table-column label="其他" align="center" prop="otherAllowance" width="80" />
      <el-table-column label="核薪小计" align="center" prop="payrollReviewSubtotal" width="100" />
      <el-table-column label="个人缴纳基本养老保险费" align="center" prop="individualBasicEndowmentInsurance" width="150" />
      <el-table-column label="个人缴纳基本医疗保险费" align="center" prop="individualBasicMedicalInsurance" width="150" />
      <el-table-column label="个人缴纳失业保险费" align="center" prop="individualUnemploymentInsurance" width="150" />
      <el-table-column label="个人社保缴纳小计" align="center" prop="individualSocialSecuritySubtotal" width="130" />
      <el-table-column label="公司社保缴纳" align="center" prop="companySocialSecurityContribution" width="120" />
      <el-table-column label="个人公积金缴纳" align="center" prop="individualHousingFund" width="120" />
      <el-table-column label="公司公积金缴纳" align="center" prop="companyHousingFund" width="120" />
      <el-table-column label="应付工资" align="center" prop="payableSalary" width="100" />
      <el-table-column label="个调税" align="center" prop="individualIncomeTax" width="90" />
      <el-table-column label="应发工资" align="center" prop="grossSalaryPayable" width="100" />
      <el-table-column label="餐补" align="center" prop="mealAllowance" width="80" />
      <el-table-column label="交补" align="center" prop="transportationAllowance" width="80" />
      <el-table-column label="其他" align="center" prop="otherFinal" width="80" />
      <el-table-column label="合计" align="center" prop="totalAmount" width="100" />
      <el-table-column label="发布状态" align="center" prop="publishStatus" width="100">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.OA_PUBLISH_STATUS" :value="scope.row.publishStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200" fixed="right">
        <template #default="scope">
          <el-button link type="primary" @click="open('update', scope.row.id)" v-hasPermi="['oa:employee-salary:update']">
            编辑
          </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)" v-hasPermi="['oa:employee-salary:delete']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <EmployeeSalaryForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { EmployeeSalaryApi, EmployeeSalaryVO } from '@/api/oa/employeesalary'
import EmployeeSalaryForm from './EmployeeSalaryForm.vue'

/** 员工工资 列表 */
defineOptions({ name: 'EmployeeSalary' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<EmployeeSalaryVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  years: undefined as string | undefined,
  month: undefined as string | undefined,
  userName: undefined,
  publishStatus: 1,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await EmployeeSalaryApi.getEmployeeSalaryPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await EmployeeSalaryApi.deleteEmployeeSalary(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await EmployeeSalaryApi.exportEmployeeSalary(queryParams)
    download.excel(data, '员工工资.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 导入按钮操作 */
const beforeImport = async (file: File) => {
  const formData = new FormData()
  formData.append('file', file)
  try {
    await EmployeeSalaryApi.importEmployeeSalary(formData)
    message.success('导入成功')
    getList()
  } catch (e) {
    message.error('导入失败')
  }
  return false // 阻止自动上传
}

/** 下载模板按钮操作 */
const downloadTemplate = async () => {
  try {
    const res = await EmployeeSalaryApi.downloadImportTemplate()
    download.excel(res, '员工工资导入模板.xls')
  } catch {
    message.error('模板下载失败')
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>